{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a4a0ea4b-22eb-49d6-a117-7e9ce3dba6af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>customer_months</th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>policy_deductable</th>\n",
       "      <th>policy_annual_premium</th>\n",
       "      <th>umbrella_limit</th>\n",
       "      <th>insured_zip</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>...</th>\n",
       "      <th>auto_make</th>\n",
       "      <th>auto_model</th>\n",
       "      <th>auto_year</th>\n",
       "      <th>fraud</th>\n",
       "      <th>policy_bind_date_year</th>\n",
       "      <th>policy_bind_date_month</th>\n",
       "      <th>policy_bind_date_day</th>\n",
       "      <th>incident_date_year</th>\n",
       "      <th>incident_date_month</th>\n",
       "      <th>incident_date_day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>189</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1465.71</td>\n",
       "      <td>5000000</td>\n",
       "      <td>455456</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>26</td>\n",
       "      <td>2000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44</td>\n",
       "      <td>234</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>821.24</td>\n",
       "      <td>0</td>\n",
       "      <td>591805</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>1996</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1844.00</td>\n",
       "      <td>0</td>\n",
       "      <td>442490</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>7</td>\n",
       "      <td>36</td>\n",
       "      <td>2002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>42</td>\n",
       "      <td>210</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>500</td>\n",
       "      <td>1867.29</td>\n",
       "      <td>0</td>\n",
       "      <td>439408</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>11</td>\n",
       "      <td>21</td>\n",
       "      <td>2003</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>29</td>\n",
       "      <td>81</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1000</td>\n",
       "      <td>816.25</td>\n",
       "      <td>0</td>\n",
       "      <td>640575</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>14</td>\n",
       "      <td>2004</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295</th>\n",
       "      <td>36</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1384.15</td>\n",
       "      <td>9000000</td>\n",
       "      <td>593323</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>27</td>\n",
       "      <td>2002</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>296</th>\n",
       "      <td>47</td>\n",
       "      <td>285</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>1590.78</td>\n",
       "      <td>7000000</td>\n",
       "      <td>447235</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>7</td>\n",
       "      <td>17</td>\n",
       "      <td>1999</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>297</th>\n",
       "      <td>39</td>\n",
       "      <td>256</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1265.24</td>\n",
       "      <td>0</td>\n",
       "      <td>592069</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>30</td>\n",
       "      <td>1997</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>298</th>\n",
       "      <td>35</td>\n",
       "      <td>54</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1229.74</td>\n",
       "      <td>0</td>\n",
       "      <td>451451</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>26</td>\n",
       "      <td>2012</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>299</th>\n",
       "      <td>34</td>\n",
       "      <td>154</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1744.33</td>\n",
       "      <td>0</td>\n",
       "      <td>462941</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>1998</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 41 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     age  customer_months  policy_state  policy_csl  policy_deductable  \\\n",
       "0     37              189             2           2               1000   \n",
       "1     44              234             1           1                500   \n",
       "2     33               23             1           2               1000   \n",
       "3     42              210             0           2                500   \n",
       "4     29               81             0           0               1000   \n",
       "..   ...              ...           ...         ...                ...   \n",
       "295   36               30             1           2               2000   \n",
       "296   47              285             2           1                500   \n",
       "297   39              256             2           2               2000   \n",
       "298   35               54             2           0                500   \n",
       "299   34              154             2           0                500   \n",
       "\n",
       "     policy_annual_premium  umbrella_limit  insured_zip  insured_sex  \\\n",
       "0                  1465.71         5000000       455456            0   \n",
       "1                   821.24               0       591805            1   \n",
       "2                  1844.00               0       442490            0   \n",
       "3                  1867.29               0       439408            1   \n",
       "4                   816.25               0       640575            0   \n",
       "..                     ...             ...          ...          ...   \n",
       "295                1384.15         9000000       593323            0   \n",
       "296                1590.78         7000000       447235            1   \n",
       "297                1265.24               0       592069            0   \n",
       "298                1229.74               0       451451            0   \n",
       "299                1744.33               0       462941            1   \n",
       "\n",
       "     insured_education_level  ...  auto_make  auto_model  auto_year  fraud  \\\n",
       "0                          5  ...          9          26       2000    0.0   \n",
       "1                          3  ...          6          10       1996    0.0   \n",
       "2                          2  ...          7          36       2002    0.0   \n",
       "3                          3  ...         11          21       2003    1.0   \n",
       "4                          4  ...          5          14       2004    0.0   \n",
       "..                       ...  ...        ...         ...        ...    ...   \n",
       "295                        1  ...          4          27       2002    NaN   \n",
       "296                        4  ...          7          17       1999    NaN   \n",
       "297                        0  ...          4          30       1997    NaN   \n",
       "298                        4  ...          9          26       2012    NaN   \n",
       "299                        5  ...          6          10       1998    NaN   \n",
       "\n",
       "     policy_bind_date_year  policy_bind_date_month  policy_bind_date_day  \\\n",
       "0                     1970                       1                     1   \n",
       "1                     1970                       1                     1   \n",
       "2                     1970                       1                     1   \n",
       "3                     1970                       1                     1   \n",
       "4                     1970                       1                     1   \n",
       "..                     ...                     ...                   ...   \n",
       "295                   1970                       1                     1   \n",
       "296                   1970                       1                     1   \n",
       "297                   1970                       1                     1   \n",
       "298                   1970                       1                     1   \n",
       "299                   1970                       1                     1   \n",
       "\n",
       "     incident_date_year  incident_date_month  incident_date_day  \n",
       "0                  1970                    1                  1  \n",
       "1                  1970                    1                  1  \n",
       "2                  1970                    1                  1  \n",
       "3                  1970                    1                  1  \n",
       "4                  1970                    1                  1  \n",
       "..                  ...                  ...                ...  \n",
       "295                1970                    1                  1  \n",
       "296                1970                    1                  1  \n",
       "297                1970                    1                  1  \n",
       "298                1970                    1                  1  \n",
       "299                1970                    1                  1  \n",
       "\n",
       "[1000 rows x 41 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd #导入数据包\n",
    "#数据预处理\n",
    "train=pd.read_csv('train.csv')\n",
    "test=pd.read_csv('test.csv')\n",
    "submission=pd.read_csv('submission.csv')\n",
    "#合并训练集和测试集同时进行数据预处理\n",
    "data=pd.concat([train,test],axis=0)\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "#标签编码\n",
    "numberical_fea=list(data.select_dtypes(include=['object']).columns)\n",
    "division_le=LabelEncoder()\n",
    "for fea in numberical_fea:\n",
    "    division_le.fit(data[fea].values)\n",
    "    data[fea]=division_le.transform(data[fea].values)\n",
    "    #拆分数据集\n",
    "testA=data[data['fraud'].isnull()].drop(['fraud'],axis=1)\n",
    "trainA=data[data['fraud'].notnull()]\n",
    "\n",
    "import datetime # 将日期列分解成年、月、日  \n",
    "data['policy_bind_date'] = pd.to_datetime(data['policy_bind_date'])  \n",
    "data['policy_bind_date_year'] = data['policy_bind_date'].dt.year.astype('int64')  \n",
    "data['policy_bind_date_month'] = data['policy_bind_date'].dt.month.astype('int64')  \n",
    "data['policy_bind_date_day'] = data['policy_bind_date'].dt.day.astype('int64')  \n",
    "data['incident_date'] = pd.to_datetime(data['incident_date'])  \n",
    "data['incident_date_year'] = data['incident_date'].dt.year.astype('int64')  \n",
    "data['incident_date_month'] = data['incident_date'].dt.month.astype('int64')  \n",
    "data['incident_date_day'] = data['incident_date'].dt.day.astype('int64')  \n",
    "data['auto_year'] = data['auto_year'].astype('int64')  \n",
    "data=data.drop(['policy_id','policy_bind_date','incident_date'],axis=1)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "179c234b-6769-4a3e-a322-2c814789e926",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>customer_months</th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>policy_deductable</th>\n",
       "      <th>policy_annual_premium</th>\n",
       "      <th>umbrella_limit</th>\n",
       "      <th>insured_zip</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>...</th>\n",
       "      <th>fraud</th>\n",
       "      <th>policy_bind_date_year</th>\n",
       "      <th>policy_bind_date_month</th>\n",
       "      <th>policy_bind_date_day</th>\n",
       "      <th>incident_date_year</th>\n",
       "      <th>incident_date_month</th>\n",
       "      <th>incident_date_day</th>\n",
       "      <th>injury_claim_per</th>\n",
       "      <th>property_claim_per</th>\n",
       "      <th>vehicle_claim_per</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>37</td>\n",
       "      <td>189</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1465.71</td>\n",
       "      <td>5000000</td>\n",
       "      <td>455456</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.109758</td>\n",
       "      <td>0.104715</td>\n",
       "      <td>0.809248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>44</td>\n",
       "      <td>234</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>821.24</td>\n",
       "      <td>0</td>\n",
       "      <td>591805</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.106077</td>\n",
       "      <td>0.200395</td>\n",
       "      <td>0.736918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1000</td>\n",
       "      <td>1844.00</td>\n",
       "      <td>0</td>\n",
       "      <td>442490</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.093249</td>\n",
       "      <td>0.193335</td>\n",
       "      <td>0.703423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>42</td>\n",
       "      <td>210</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>500</td>\n",
       "      <td>1867.29</td>\n",
       "      <td>0</td>\n",
       "      <td>439408</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.160364</td>\n",
       "      <td>0.086477</td>\n",
       "      <td>0.777613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>29</td>\n",
       "      <td>81</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1000</td>\n",
       "      <td>816.25</td>\n",
       "      <td>0</td>\n",
       "      <td>640575</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.094046</td>\n",
       "      <td>0.172471</td>\n",
       "      <td>0.695677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>295</th>\n",
       "      <td>36</td>\n",
       "      <td>30</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1384.15</td>\n",
       "      <td>9000000</td>\n",
       "      <td>593323</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.215221</td>\n",
       "      <td>0.105835</td>\n",
       "      <td>0.740848</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>296</th>\n",
       "      <td>47</td>\n",
       "      <td>285</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>500</td>\n",
       "      <td>1590.78</td>\n",
       "      <td>7000000</td>\n",
       "      <td>447235</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.121959</td>\n",
       "      <td>0.122569</td>\n",
       "      <td>0.753621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>297</th>\n",
       "      <td>39</td>\n",
       "      <td>256</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2000</td>\n",
       "      <td>1265.24</td>\n",
       "      <td>0</td>\n",
       "      <td>592069</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.136500</td>\n",
       "      <td>0.129832</td>\n",
       "      <td>0.822949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>298</th>\n",
       "      <td>35</td>\n",
       "      <td>54</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1229.74</td>\n",
       "      <td>0</td>\n",
       "      <td>451451</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.194537</td>\n",
       "      <td>0.095115</td>\n",
       "      <td>0.772917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>299</th>\n",
       "      <td>34</td>\n",
       "      <td>154</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>500</td>\n",
       "      <td>1744.33</td>\n",
       "      <td>0</td>\n",
       "      <td>462941</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1970</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.108301</td>\n",
       "      <td>0.109533</td>\n",
       "      <td>0.775426</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 44 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     age  customer_months  policy_state  policy_csl  policy_deductable  \\\n",
       "0     37              189             2           2               1000   \n",
       "1     44              234             1           1                500   \n",
       "2     33               23             1           2               1000   \n",
       "3     42              210             0           2                500   \n",
       "4     29               81             0           0               1000   \n",
       "..   ...              ...           ...         ...                ...   \n",
       "295   36               30             1           2               2000   \n",
       "296   47              285             2           1                500   \n",
       "297   39              256             2           2               2000   \n",
       "298   35               54             2           0                500   \n",
       "299   34              154             2           0                500   \n",
       "\n",
       "     policy_annual_premium  umbrella_limit  insured_zip  insured_sex  \\\n",
       "0                  1465.71         5000000       455456            0   \n",
       "1                   821.24               0       591805            1   \n",
       "2                  1844.00               0       442490            0   \n",
       "3                  1867.29               0       439408            1   \n",
       "4                   816.25               0       640575            0   \n",
       "..                     ...             ...          ...          ...   \n",
       "295                1384.15         9000000       593323            0   \n",
       "296                1590.78         7000000       447235            1   \n",
       "297                1265.24               0       592069            0   \n",
       "298                1229.74               0       451451            0   \n",
       "299                1744.33               0       462941            1   \n",
       "\n",
       "     insured_education_level  ...  fraud  policy_bind_date_year  \\\n",
       "0                          5  ...    0.0                   1970   \n",
       "1                          3  ...    0.0                   1970   \n",
       "2                          2  ...    0.0                   1970   \n",
       "3                          3  ...    1.0                   1970   \n",
       "4                          4  ...    0.0                   1970   \n",
       "..                       ...  ...    ...                    ...   \n",
       "295                        1  ...    NaN                   1970   \n",
       "296                        4  ...    NaN                   1970   \n",
       "297                        0  ...    NaN                   1970   \n",
       "298                        4  ...    NaN                   1970   \n",
       "299                        5  ...    NaN                   1970   \n",
       "\n",
       "     policy_bind_date_month  policy_bind_date_day  incident_date_year  \\\n",
       "0                         1                     1                1970   \n",
       "1                         1                     1                1970   \n",
       "2                         1                     1                1970   \n",
       "3                         1                     1                1970   \n",
       "4                         1                     1                1970   \n",
       "..                      ...                   ...                 ...   \n",
       "295                       1                     1                1970   \n",
       "296                       1                     1                1970   \n",
       "297                       1                     1                1970   \n",
       "298                       1                     1                1970   \n",
       "299                       1                     1                1970   \n",
       "\n",
       "     incident_date_month  incident_date_day  injury_claim_per  \\\n",
       "0                      1                  1          0.109758   \n",
       "1                      1                  1          0.106077   \n",
       "2                      1                  1          0.093249   \n",
       "3                      1                  1          0.160364   \n",
       "4                      1                  1          0.094046   \n",
       "..                   ...                ...               ...   \n",
       "295                    1                  1          0.215221   \n",
       "296                    1                  1          0.121959   \n",
       "297                    1                  1          0.136500   \n",
       "298                    1                  1          0.194537   \n",
       "299                    1                  1          0.108301   \n",
       "\n",
       "     property_claim_per  vehicle_claim_per  \n",
       "0              0.104715           0.809248  \n",
       "1              0.200395           0.736918  \n",
       "2              0.193335           0.703423  \n",
       "3              0.086477           0.777613  \n",
       "4              0.172471           0.695677  \n",
       "..                  ...                ...  \n",
       "295            0.105835           0.740848  \n",
       "296            0.122569           0.753621  \n",
       "297            0.129832           0.822949  \n",
       "298            0.095115           0.772917  \n",
       "299            0.109533           0.775426  \n",
       "\n",
       "[1000 rows x 44 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#新增索赔金额占比\n",
    "data['injury_claim_per']=data['injury_claim']/data['total_claim_amount']\n",
    "data['property_claim_per']=data['property_claim']/data['total_claim_amount']\n",
    "data['vehicle_claim_per']=data['vehicle_claim']/data['total_claim_amount']\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "be9d553e-7190-4228-a324-0b82c9f68e88",
   "metadata": {},
   "outputs": [],
   "source": [
    "#过滤重要特征\n",
    "train_2=trainA[new_features]\n",
    "test_2=testA[new_features]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a7294074-415f-4748-a8de-8d7c540b2368",
   "metadata": {},
   "outputs": [],
   "source": [
    "#拆分训练集和测试集   \n",
    "test_2=data[data['fraud'].isnull()].drop(['fraud'],axis=1)  \n",
    "train_2=data[data['fraud'].notnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e5449ac9-56f4-40ee-a921-1c724cd7ab3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "经过皮尔逊相关系数选择后的数据集的形状: (700, 15)\n",
      "选择的特征索引: Index(['policy_deductable', 'insured_education_level', 'insured_hobbies',\n",
      "       'incident_type', 'collision_type', 'incident_severity',\n",
      "       'authorities_contacted', 'incident_state', 'incident_city',\n",
      "       'number_of_vehicles_involved', 'witnesses', 'total_claim_amount',\n",
      "       'injury_claim', 'property_claim', 'vehicle_claim'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# 特征选择\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "from sklearn.feature_selection import f_regression\n",
    "\n",
    "# 创建皮尔逊相关系数选择器对象，设定选择的特征数量\n",
    "selector = SelectKBest(f_regression, k=15)\n",
    "\n",
    "# 拟合数据并进行特征选择\n",
    "X = trainA.drop(['fraud'], axis=1)\n",
    "Y = trainA['fraud']\n",
    "X_selected = selector.fit_transform(X, Y)\n",
    "\n",
    "# 查看选择后的特征\n",
    "print(\"经过皮尔逊相关系数选择后的数据集的形状:\", X_selected.shape)\n",
    "mask = selector.get_support() # 获得特征掩码\n",
    "new_features = X.columns[mask] # 选择重要的特征\n",
    "print(\"选择的特征索引:\", new_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1db97253-cc03-43bd-bcdf-870aade78e82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[LightGBM] [Warning] Unknown parameter: bum_leaves\n",
      "[LightGBM] [Warning] Unknown parameter: bum_leaves\n",
      "[LightGBM] [Info] Number of positive: 181, number of negative: 519\n",
      "[LightGBM] [Info] Auto-choosing col-wise multi-threading, the overhead of testing was 0.000327 seconds.\n",
      "You can set `force_col_wise=true` to remove the overhead.\n",
      "[LightGBM] [Info] Total Bins 2113\n",
      "[LightGBM] [Info] Number of data points in the train set: 700, number of used features: 34\n",
      "[LightGBM] [Info] [binary:BoostFromScore]: pavg=0.258571 -> initscore=-1.053407\n",
      "[LightGBM] [Info] Start training from score -1.053407\n",
      "[LightGBM] [Warning] Unknown parameter: bum_leaves\n"
     ]
    }
   ],
   "source": [
    "import lightgbm as lgb\n",
    "model_lgb=lgb.LGBMClassifier(\n",
    "    bum_leaves=2**5-1,reg_alpha=0.25,reg_lambda=0.25,objective='binary',\n",
    "    max_depth=-1,learning_rate=0.005,min_child_samples=3,random_state=2022,\n",
    "    n_estimators=2000,subsample=1,colsample_bytree=1,\n",
    ")\n",
    "#模型训练\n",
    "#过滤数值型特征\n",
    "#Nu_feature=list(train.select_dtypes(exclude=['object']).columns)#数值变量\n",
    "#train_new=train[Nu_feature]\n",
    "\n",
    "#Nu_feature=Nu_feature[0:-1]\n",
    "#test_new=test[Nu_feature]\n",
    "\n",
    "model_lgb.fit(train_2.drop(['fraud'],axis=1),train_2['fraud'])\n",
    "#AUC测评:以proba进行提交，结果会更好\n",
    "y_pred=model_lgb.predict_proba(test_2)\n",
    "result=pd.read_csv('submission.csv')\n",
    "result['fraud']=y_pred[:,1]\n",
    "result.to_csv('baseline2.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "60672dce-63e2-42d6-bb59-d1abc7811562",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_2=data[data['fraud'].notnull()]\n",
    "x_train_2=train_2.drop(['fraud'],axis=1)\n",
    "y_train_2=train_2['fraud']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c7501e27-f8ff-4bf5-a6ec-867efad94037",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率为： 0.7857142857142857\n",
      "精确率为： 0.5925925925925926\n",
      "召回率为： 0.5818181818181818\n",
      "F1为： 0.5871559633027522\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import precision_score\n",
    "from sklearn.metrics import recall_score\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_val, y_train, y_val = train_test_split(x_train_2,y_train_2, test_size=0.3, random_state=42)\n",
    "model_lgb = lgb.LGBMClassifier(verbosity=-1)\n",
    "# 模型训练，适用默认参数\n",
    "model_lgb.fit(X_train, y_train)\n",
    "# AUC评测：以proba进行提交，结果会更好\n",
    "y_pred = model_lgb.predict(X_val)\n",
    "print(\"准确率为：\" ,accuracy_score(y_val, y_pred))\n",
    "print(\"精确率为：\" ,precision_score(y_val, y_pred))\n",
    "print(\"召回率为：\" ,recall_score(y_val, y_pred))\n",
    "print(\"F1为：\" ,f1_score(y_val, y_pred))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "267a2882-6b04-45ea-80d9-21838debcf3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       0.85      0.86      0.86       155\n",
      "         1.0       0.59      0.58      0.59        55\n",
      "\n",
      "    accuracy                           0.79       210\n",
      "   macro avg       0.72      0.72      0.72       210\n",
      "weighted avg       0.78      0.79      0.79       210\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_val, y_train, y_val = train_test_split(x_train_2,y_train_2, test_size=0.3, random_state=42)\n",
    "model_lgb = lgb.LGBMClassifier(verbosity=-1)\n",
    "model_lgb.fit(X_train, y_train)\n",
    "y_pred = model_lgb.predict(X_val)\n",
    "r=classification_report(y_val, y_pred)\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "00051cee-acbe-44db-99e4-35fe1d8c183b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.719941348973607\n",
      "0.8109090909090909\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split  \n",
    "from sklearn.metrics import roc_auc_score  \n",
    "  \n",
    "X_train, X_val, y_train, y_val = train_test_split(x_train_2, y_train_2, test_size=0.3, random_state=42)  \n",
    "  \n",
    "model_lgb = lgb.LGBMClassifier(verbosity=-1)  \n",
    "model_lgb.fit(X_train, y_train)  \n",
    "y_pred = model_lgb.predict(X_val)  \n",
    "y_prob = model_lgb.predict_proba(X_val)[::,1]  \n",
    "  \n",
    "auc1 = roc_auc_score(y_val, y_pred)  \n",
    "print(auc1)  \n",
    "auc2 = roc_auc_score(y_val, y_prob)  \n",
    "print(auc2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b15b52ab-89f4-4d75-8852-b4b7bb58c12f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均AUC为： 0.8223279673583678\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score  \n",
    "from sklearn.model_selection import KFold  \n",
    "#初始化模型  \n",
    "model_lgb = lgb.LGBMClassifier(verbosity=-1)  \n",
    "#将数据集别分为5个新叠，对数据进行洗牌，并设置随机种子为0  \n",
    "kf = KFold(n_splits=5, shuffle=True, random_state=0)  \n",
    "lgb_scores = cross_val_score(model_lgb, x_train_2, y_train_2, cv=kf, scoring='roc_auc')  \n",
    "print(\"平均AUC为：\" , lgb_scores.mean())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "272d7c15-6bd1-45d8-9eb8-a2fab86ff556",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AUC为: 0.8378739316239316\n",
      "AUC为: 0.8179800221975582\n",
      "AUC为: 0.8265547100498557\n",
      "AUC为: 0.8378378378378378\n",
      "AUC为: 0.7913933350826554\n",
      "平均AUC为: 0.8223279673583678\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import KFold\n",
    "import lightgbm as lgb\n",
    "from sklearn.metrics import roc_auc_score,roc_curve,auc\n",
    "\n",
    "#5折交叉验证\n",
    "kf = KFold(n_splits = 5, shuffle=True, random_state=0)\n",
    "score=0\n",
    "\n",
    "for train_index, test_index in kf.split(x_train_2):\n",
    "    X_train, X_test = x_train_2.iloc[train_index], x_train_2.iloc[test_index]\n",
    "    y_train, y_test = y_train_2.iloc[train_index], y_train_2.iloc[test_index]\n",
    "    clf = lgb.LGBMClassifier(verbosity=-1).fit(X_train, y_train)\n",
    "    test_auc = roc_auc_score(y_test, clf.predict_proba(X_test)[:,1])\n",
    "    print(\"AUC为:\", test_auc)\n",
    "    score = score + test_auc\n",
    "\n",
    "avg_auc = score / 5\n",
    "print(\"平均AUC为:\", avg_auc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e1c46e5-cfbf-4f42-9219-946e23f1d49a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
